package com.personal.sfe.user.controlelr;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.personal.sfe.common.Result;
import com.personal.sfe.user.entity.User;
import com.personal.sfe.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 梁奔
 * @date: 2025/11/05
 */

@RestController
@RequestMapping("/api/user")
public class UseController {

    @Autowired
    private UserService userService;


    @Cacheable(value = "sfe:user", key = "T(com.personal.sfe.util.CacheKeyUtils).toKey(#p0)", unless = "#result == null")
    @PostMapping("/list")
    public Result listUser(@RequestBody User user){
        LambdaQueryWrapper<User> eq = Wrappers.lambdaQuery(User.class);
        if (StrUtil.isNotEmpty(user.getUsername())){
            eq.eq(User::getUsername, user.getUsername());
        }
        List<User> list = userService.list(eq);
        return Result.ok("success", list);
    }
}
